package com.hengxinguotong.zhihuichengjian.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.BatchAddTeamAdapter;
import com.hengxinguotong.zhihuichengjian.bean.Company;
import com.hengxinguotong.zhihuichengjian.bean.Team;
import com.hengxinguotong.zhihuichengjian.ui.engineering.task.ServiceCompanyActivity;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import com.hengxinguotong.zhihuichengjian.widget.view.SelectListPop;
import com.lidroid.xutils.http.RequestParams;

import org.apache.http.entity.StringEntity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 新的添加班组界面
 * Created by liux on 2017/4/20.
 */

public class AddTeamActivity extends BaseActivity implements BatchAddTeamAdapter.OnDeleteClickListener{
    @BindView(R.id.title_tv)
    HXTextView titleTv;
    @BindView(R.id.iv_back)
    HXTextView ivBack;
    @BindView(R.id.submit)
    HXTextView submit;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.name_etv)
    HXTextView nameEtv;
    @BindView(R.id.company_tv)
    HXTextView companyTv;
    @BindView(R.id.type_rl)
    RelativeLayout typeRl;
    @BindView(R.id.main_ll)
    LinearLayout mainLl;
    @BindView(R.id.group_lv)
    ListView groupLv;
    private SelectListPop mSelectListPop;
    private Company company;
    private List<Team> teamList;
    private BatchAddTeamAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addteam);
        ButterKnife.bind(this);
        initList();
    }

    private void initList(){
        teamList = new ArrayList<>();
        adapter = new BatchAddTeamAdapter(this, teamList);
        groupLv.setAdapter(adapter);
        adapter.setOnDeleteClickListener(this);
    }

    private void addTeam() {
        if (teamList == null || teamList.size() <= 0) {
            showToast("请选择班组");
            return;
        }

        if (company == null) {
            showToast("请选择所属公司");
            return;
        }

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("constructionId", SPUtil.getString(this, "constructionid"));
            jsonObject.put("projectCompanyId", company.getId());
            JSONArray jsonArray = new JSONArray();
            for(int i=0;i<teamList.size();i++) {
                JSONObject object = new JSONObject();
                object.put("name",teamList.get(i).getName());
                object.put("value",teamList.get(i).getValue());
                jsonArray.put(object);
            }
            jsonObject.put("teamNameList", jsonArray);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestParams params = new RequestParams();
        params.addHeader("Content-type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
            String str=jsonObject.toString();
            Log.i("dsf", str);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        Utils.requestData(getResources().getString(R.string.loading), this, "/team/batchAddTeam/" + SPUtil.getString(this, "uuid"), params, new Utils.ListenerJson() {
            @Override
            public void onSuccess(String json) {
                showToast("提交成功");
                setResult(1);
                finish();
            }

            @Override
            public void onFailure(String json) {

            }
        });

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {
            return;
        }
        if (resultCode == 1) {
            company = (Company) data.getSerializableExtra("company");
            companyTv.setText(company.getName());
        }
        if(resultCode == 2){
            teamList = (List<Team>) data.getSerializableExtra("teamList");
            adapter.setTeamList(teamList);
        }
    }

    @OnClick({R.id.iv_back, R.id.submit, R.id.company_tv, R.id.name_etv})
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.submit:
                addTeam();
                break;
            case R.id.company_tv:
                intent = new Intent(this, ServiceCompanyActivity.class);
                intent.putExtra("companyType",ServiceCompanyActivity.FROMTEAM);
                intent.putExtra("type",ServiceCompanyActivity.COMMONSERVICECOMPANY);
                startActivityForResult(intent, 1);
                break;
            case R.id.name_etv:
                intent = new Intent(this, TeamListActivity.class);
                startActivityForResult(intent, 1);
                break;
        }
    }

    @Override
    public void onClick(int posision) {
        teamList.remove(posision);
        adapter.setTeamList(teamList);
    }
}
